﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;

namespace LotteCinema.Admin
{
    public partial class QuanLyLichChieuPhim : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
                laydslcp();
        }

        public DataTable Timkiem_MA(string ten)
        {
            try
            {
                if (ten != null || ten != "")
                {
                    List<SqlParameter> sqlPara = new List<SqlParameter>();
                    sqlPara.Add(new SqlParameter("@MAlcp", ten));
                    return CDatastore.DocStore("SP_TIMKIEM_LICHCHIEUPHIM_MALCP", sqlPara);
                }
            }
            catch (Exception e)
            {
                lblthongbao.Text = e.Message;
            }
            return CDatastore.DocStore("SP_TIMKIEM_LICHCHIEUPHIM_MALCP");
        }

        public DataTable Timkiem_SC(string ten)
        {
            try
            {
                if (ten != null || ten != "")
                {
                    List<SqlParameter> sqlPara = new List<SqlParameter>();
                    sqlPara.Add(new SqlParameter("@MAsc", ten));
                    return CDatastore.DocStore("SP_TIMKIEM_SUATCHIEU_MASC", sqlPara);
                }
            }
            catch (Exception e)
            {
                lblthongbao.Text = e.Message;
            }
            return CDatastore.DocStore("SP_TIMKIEM_SUATCHIEU_MASC");
        }

        public DataTable Timkiem_PC(string ten)
        {
            try
            {
                if (ten != null || ten != "")
                {
                    List<SqlParameter> sqlPara = new List<SqlParameter>();
                    sqlPara.Add(new SqlParameter("@MAPhong", ten));
                    return CDatastore.DocStore("SP_TIMKIEM_PHONGCHIEU_MAPHONG", sqlPara);
                }
            }
            catch (Exception e)
            {
                lblthongbao.Text = e.Message;
            }
            return CDatastore.DocStore("SP_TIMKIEM_PHONGCHIEU_MAPHONG");
        }


        public void laydslcp()
        {
            string str;
            DataTable dt = Timkiem_MA("");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                str = dt.Rows[i]["MaLCP"].ToString();
                ddrMaLCP.Items.Add(str);
            }
            dt = Timkiem_SC("");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                str = dt.Rows[i]["MaSC"].ToString();
                ddlMaSC.Items.Add(str);
            }
            dt = Timkiem_PC("");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                str = dt.Rows[i]["MaPhong"].ToString();
                ddlMaPhong.Items.Add(str);
            }
            dt = Timkiem_MA("");
            grLCP.DataSource = dt;
            grLCP.DataBind();
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddrMaLCP.Text = grLCP.SelectedRow.Cells[0].Text;
            ddlMaSC.Text = grLCP.SelectedRow.Cells[1].Text;
            ddlMaPhong.Text = grLCP.SelectedRow.Cells[2].Text;
            txtThoiGian.Text = grLCP.SelectedRow.Cells[3].Text.ToString();
        }

        protected void ddrMaLCP_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataTable dt = Timkiem_MA(ddrMaLCP.SelectedItem.ToString());
            if (dt.Rows.Count != 0)
            {
                ddlMaSC.Text = dt.Rows[0][1].ToString();
                ddlMaPhong.Text = dt.Rows[0][2].ToString();
                txtThoiGian.Text = dt.Rows[0][3].ToString();
            }
        }

        protected void imThem_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                List<SqlParameter> sqlPara = new List<SqlParameter>();
                sqlPara.Add(new SqlParameter("@masc", ddlMaSC.Text.ToString()));
                sqlPara.Add(new SqlParameter("@maphong", ddlMaPhong.Text.ToString()));
                sqlPara.Add(new SqlParameter("@thoigian", DateTime.Parse(txtThoiGian.Text.ToString())));
                CDatastore.ThucHienStore("SP_THEM_LICHCHIEUPHIM", sqlPara);
                lblthongbao.Text = "Bạn đã thêm Lịch Chiếu Phim thành công";
                ddrMaLCP.Items.Clear();
                ddlMaSC.Items.Clear();
                ddlMaPhong.Items.Clear();
                txtThoiGian.Text = null;
                laydslcp();
            }
            catch (Exception ex)
            {
                lblthongbao.Text = ex.Message;
            }
            
        }

        protected void imXoa_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                List<SqlParameter> sqlPara = new List<SqlParameter>();
                sqlPara.Add(new SqlParameter("@malcp", ddrMaLCP.Text.ToString()));
                CDatastore.ThucHienStore("SP_DELETE_LICHCHIEUPHIM", sqlPara);
                lblthongbao.Text = "Bạn đã xóa Mã Lịch Chiếu Phim " + ddrMaLCP.Text + " thành công";
                ddrMaLCP.Items.Clear();
                ddlMaSC.Items.Clear();
                ddlMaPhong.Items.Clear();
                txtThoiGian.Text = null;
                laydslcp();

            }
            catch (Exception ex)
            {
                lblthongbao.Text = ex.Message;
            }
        }

        protected void imSua_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                List<SqlParameter> sqlPara = new List<SqlParameter>();
                sqlPara.Add(new SqlParameter("@malcp", ddrMaLCP.Text.ToString()));
                sqlPara.Add(new SqlParameter("@masc", ddlMaSC.Text.ToString()));
                sqlPara.Add(new SqlParameter("@maphong", ddlMaPhong.Text.ToString()));
                sqlPara.Add(new SqlParameter("@thoigian", DateTime.Parse(txtThoiGian.Text.ToString())));
                CDatastore.ThucHienStore("SP_UPDATE_LICHCHIEUPHIM", sqlPara);
                lblthongbao.Text = "Bạn đã cập nhật Lịch Chiếu Phim " + ddrMaLCP.Text + " thành công";
                ddrMaLCP.Items.Clear();
                ddlMaSC.Items.Clear();
                ddlMaPhong.Items.Clear();
                txtThoiGian.Text = null;
                laydslcp();
            }
            catch (Exception ex)
            {
                lblthongbao.Text = ex.Message;
            }
        }

        protected void grLCP_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grLCP.PageIndex = e.NewPageIndex;
            grLCP.DataBind();
            laydslcp();
        }



    }
}